package com.ruijie.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import redis.clients.jedis.JedisPoolConfig;


@Configuration
@EnableCaching
public class CacheConfig {

	@Bean
	public JedisConnectionFactory jedisConnectionFactory(){
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setUsePool(true);
		jedisConnectionFactory.setHostName("localhost");
		jedisConnectionFactory.setPort(6379);
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(10);
		config.setMaxTotal(100);
		config.setTestOnBorrow(true);
		jedisConnectionFactory.setPoolConfig(config);
		return jedisConnectionFactory;
	}
	
	@Bean
	public RedisTemplate<?,?> redisTemplate(RedisConnectionFactory redisConnectionFactory){
		RedisTemplate<?,?> redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(redisConnectionFactory);
		redisTemplate.setEnableTransactionSupport(true);
//		redisTemplate.setDefaultSerializer(new Jackson2JsonRedisSerializer(User.class));
		return redisTemplate;
	}
	
	@Bean
	public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate){
		CacheManager cacheManager = new RedisCacheManager(redisTemplate);
		return cacheManager;
	}
}
